import { AppUtils } from './../api/AppUtils';
import { AdvertiseVO } from './../vo/AdvertiseVO';
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { ApiService } from '../api/api.service';
import { ApiUrl } from '../api/ApiUrl';
import { AlertController } from '@ionic/angular';
import { ArticleVO } from '../vo/ArticleVO';
import { Router } from '@angular/router';

@Component({
  selector: 'app-newsdynamic',
  templateUrl: './newsdynamic.page.html',
  styleUrls: ['./newsdynamic.page.scss'],
})
export class NewsdynamicPage implements OnInit {
  slideItems: AdvertiseVO[] = [
    { 'iid': 1, 'ccontent': 'title1', 'cpicpath': 'assets/lb/lb02.png', 'cskiplinks': '' },
    { 'iid': 2, 'ccontent': 'title2', 'cpicpath': 'assets/lb/lb01.png', 'cskiplinks': '' },
    { 'iid': 3, 'ccontent': 'title3', 'cpicpath': 'assets/lb/lb03.png', 'cskiplinks': '' }
  ];
  constructor(private router: Router, public api: ApiService, public activatedRoute: ActivatedRoute,
    public alertController: AlertController, private appUtils: AppUtils) { }
  citems: ArticleVO[] = [];
  bitems: ArticleVO[] = [];
  items: ArticleVO[] = [];
  ngOnInit() {
    this.onSearch();
    // 加载轮播图信息
    this.api.getItems<AdvertiseVO>(ApiUrl.lbt, { 'ilocation': 3 }).subscribe(response => {
      if (response != null || response.length > 0) {
        this.slideItems = response;
        // 遍历拼接url
        this.slideItems.forEach(element => {
          element.cpicpath = ApiUrl.baseUrl + element.cpicpath;
        });
      }
    });
  }
  goToMore(itype) {
    this.router.navigate(['/companydynamic', { 'itype': itype }]);
  }
  goToDetails(iid) {
    this.router.navigate(['/article', { 'iid': iid }]);
  }
  onSearch() {
    this.api.getItems<ArticleVO>(ApiUrl.getArticle, {}).subscribe(response => {
      if (response === null || response.length === 0) {
        console.log('没有数据返回');
        return;
      }
      this.items = response;
      for (let i = 0; i < this.items.length; i++) {
        if (this.items[i].articletypeiid === 2) {
          this.citems.push(this.items[i]);
        }
        if (this.items[i].articletypeiid === 1) {
          this.bitems.push(this.items[i]);
        }
      }
    }, () => {

      console.log('出错了');
    });
  }
  // 打开浏览器页面
  toWebView(curl, ctitle) {
    this.appUtils.toWebView(curl, ctitle);
  }
}
